package cn.gemiman.daoyi.module.cms.controller.admin.articledetail;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gemiman.daoyi.framework.common.pojo.PageParam;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;

import cn.gemiman.daoyi.framework.excel.core.util.ExcelUtils;

import cn.gemiman.daoyi.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gemiman.daoyi.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.gemiman.daoyi.module.cms.controller.admin.articledetail.vo.*;
import cn.gemiman.daoyi.module.cms.dal.dataobject.articledetail.ArticleDetailDO;
import cn.gemiman.daoyi.module.cms.service.articledetail.ArticleDetailService;

@Tag(name = "管理后台 - 文章")
@RestController
@RequestMapping("/cms/article-detail")
@Validated
public class ArticleDetailController {

    @Resource
    private ArticleDetailService articleDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建文章")
    @PreAuthorize("@ss.hasPermission('cms:article-detail:create')")
    public CommonResult<Long> createArticleDetail(@Valid @RequestBody ArticleDetailSaveReqVO createReqVO) {
        return success(articleDetailService.createArticleDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文章")
    @PreAuthorize("@ss.hasPermission('cms:article-detail:update')")
    public CommonResult<Boolean> updateArticleDetail(@Valid @RequestBody ArticleDetailSaveReqVO updateReqVO) {
        articleDetailService.updateArticleDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文章")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:article-detail:delete')")
    public CommonResult<Boolean> deleteArticleDetail(@RequestParam("id") Long id) {
        articleDetailService.deleteArticleDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文章")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:article-detail:query')")
    public CommonResult<ArticleDetailRespVO> getArticleDetail(@RequestParam("id") Long id) {
        ArticleDetailDO articleDetail = articleDetailService.getArticleDetail(id);
        return success(BeanUtils.toBean(articleDetail, ArticleDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文章分页")
    @PreAuthorize("@ss.hasPermission('cms:article-detail:query')")
    public CommonResult<PageResult<ArticleDetailRespVO>> getArticleDetailPage(@Valid ArticleDetailPageReqVO pageReqVO) {
        PageResult<ArticleDetailDO> pageResult = articleDetailService.getArticleDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ArticleDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文章 Excel")
    @PreAuthorize("@ss.hasPermission('cms:article-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportArticleDetailExcel(@Valid ArticleDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ArticleDetailDO> list = articleDetailService.getArticleDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文章.xls", "数据", ArticleDetailRespVO.class,
                        BeanUtils.toBean(list, ArticleDetailRespVO.class));
    }

}